<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-adx__configlinker}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button   type="success" @click="upAndDown(1)">批量上架 </el-button>
        </el-form-item>
        <el-form-item>
          <el-button  type="info" @click="upAndDown(2)">批量下架 </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:configlinker:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('adx:configlinker:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" label="序号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="number" label="访问数" header-align="center" align="center">
               <template slot-scope="scope">
                <span style="color: green;">
                {{ scope.row.number}}/
            </span>
            <span style="color: red;">
              {{scope.row.consumeNumber||0}}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" header-align="center" align="center">
            <template slot-scope="scope">
                <span :style="{color: scope.row.status === '1' ? 'red' : scope.row.status === '2' ? 'green' : ''}">
                {{ $getDictLabel("tb_status", scope.row.status) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="linker" label="链接" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('adx:configlinker:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('adx:configlinker:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
              <el-button  type="text" size="small" @click="copyData(scope.row.id)">复制</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './configlinker-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/adx/configlinker/page',
        getDataListIsPage: true,
        exportURL: '/adx/configlinker/export',
        deleteURL: '/adx/configlinker',
        deleteIsBatch: true
      },
      dataForm: {
        id: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods:{

upAndDown(status) {
    if (this.dataListSelections.length <= 0) {
        return this.$message({
            message: "请至少选择一条数据",
            type: 'warning',
            duration: 500
        });
    }
    
    const params = {
        ids: this.dataListSelections.map(item => item[this.mixinViewModuleOptions.deleteIsBatchKey]),
        status: status
    };
    
    this.$http.post('/adx/configlinker/upAndDown', params)
        .then((res) => {  
            if (res.data.code !== 0) {  // 访问 res.data.code 而不是 res.code
                this.$message.error(res.data.msg);
            } else {
                this.$message.success('操作成功');
                // 可选：刷新数据
                this.getDataList();
            }
        })
        .catch((error) => {
            console.error('操作失败:', error);
            this.$message.error('操作失败，请稍后重试');
        });
},

   copyData(id){
      //复制账户的功能
      this.$confirm('确认要复制当前配置吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
               return this.$http.get('adx/configlinker/copyLinker?id='+id).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.getDataList()
              this.$message('数据复制成功')
               })
          }).catch(() => {
                   
        });
    },


    }
  }
</script>
